//----------------------------------
//-----Heckler & Koch MP5 SWEP------
//----------Made by Brian Nevec-----
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

//if on server
if SERVER then

	//add the file
	AddCSLuaFile("shared.lua");

	//set the hold type
	SWEP.HoldType			= "smg";

end

//if on client
if CLIENT then

	//set some stuff
	SWEP.PrintName			= "Heckler & Koch MP5";
	SWEP.Author				= "Brian Nevec";
	SWEP.Slot				= 2;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Fills their asses with bullets";
	SWEP.Instructions		= "Left Click: Fire, Right Click: Change mode";

	//set the category
	SWEP.Category			= "SMOD";
	
	//selection icon
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/hecklericon");

	-- Kill Icon
	if(file.Exists("../materials/weapons/heckler_killicon.vmt")) then

		killicon.Add("weapon_hk_mp5","weapons/heckler_killicon",Color(255,255,255));

	end 

end

//is it spawnable?
SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

//models
SWEP.ViewModel				= "models/weapons/v_mp5.mdl";
SWEP.WorldModel				= "models/weapons/w_mp5.mdl";

//more stuff
SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

//primary settings
SWEP.Primary =
{
	ClipSize				= 31,
	DefaultClip				= 60,
	Automatic				= true,
	Ammo					= "SMG1"
}

//secondary settings
SWEP.Secondary =
{
	ClipSize				= 3,
	DefaultClip				= 3,
	Automatic				= false,
	Ammo					= "SMG1_Grenade"
}

SWEP.BobScale = 1.5;
SWEP.SwayScale = 2;

SWEP.Sounds =
{
	Fire1 = Sound("weapons/mp5/mp5-1.wav"),
	Fire2 = Sound("weapons/mp5/mp5-npc.wav"),
	ClipIn = Sound("weapons/mp5/mp5_clipin.wav"),
	ClipOut = Sound("weapons/mp5/mp5_clipout.wav"),
	SlideBack = Sound("weapons/mp5/mp5_slideback.wav"),
	FireGrenade = Sound("weapons/grenade_launcher1.wav")
}

//1 - normal, 2 - grenade, 3 - ironsights(fires bullets)
SWEP.CurMode = 1;

//on initialize
function SWEP:Initialize()

	//if on server
	if SERVER then

		//set the hold type
		self:SetWeaponHoldType(self.HoldType);

	end

end

//when we want to attack with the primary
function SWEP:PrimaryAttack()

	if self.CurMode == 2 then

		if self.Weapon:CanSecondaryAttack() then

			local Gren = ents.Create("smod_grenade");

			if ValidEntity(Gren) then

				Gren:SetPos(self.Owner:GetShootPos());
				Gren:SetAngles(Angle(math.Rand(1,360),math.Rand(1,360),math.Rand(1,360)));
				Gren:SetOwner(self.Owner);

				Gren:Spawn();
				Gren:Activate();

				local Physics = Gren:GetPhysicsObject();

				if ValidEntity(Physics) then

					local Dir = self.Owner:GetAimVector():Angle();
					Dir:RotateAroundAxis(Dir:Right(),3);
					Dir = Dir:Forward();

					Physics:ApplyForceCenter(Dir * 7000);

					self.Owner:EmitSound(self.Sounds.FireGrenade);

					self.Weapon:TakeSecondaryAmmo(1);

					self.Owner:ViewPunch(Angle(math.Rand(-5,5),math.Rand(-5,5),math.Rand(-5,5)));
					
				else

					Gren:Remove();

				end

			end

		end

	else

		if self.Weapon:CanPrimaryAttack() then

			//create a bullet
			local Bullet = {};
			Bullet.Src = self.Owner:GetShootPos();
			Bullet.Attacker = self.Owner;
			Bullet.Dir = self.Owner:GetAimVector();
			Bullet.Spread = Vector(0.02,0.02,0);
			Bullet.Num = 1;
			Bullet.Damage = 12;
			Bullet.Force = 5;
			Bullet.Tracer = 1;
			Bullet.TracerName = "Tracer";

			//fire it
			self.Weapon:FireBullets(Bullet);
			self.Weapon:ShootEffects();

			//emit sound
			self.Owner:EmitSound(self.Sounds["Fire"..math.random(1,2)]);

			//take off some ammo
			self.Weapon:TakePrimaryAmmo(1);
			
			self.Owner:ViewPunch(Angle(math.Rand(-1,1),math.Rand(-1,1),math.Rand(-1,1)));

		end

	end

	if self.CurMode == 2 then

		//delay the fire
		self.Weapon:SetNextPrimaryFire(CurTime() + 1.5);

	else

		//delay the fire
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.09);

	end

	//and return false
	return false;

end

function SWEP:Reload()

	if self.Weapon:Clip1() < 31 then

		self.Weapon:DefaultReload(ACT_VM_RELOAD);

		self.Owner:EmitSound(self.Sounds.ClipOut);

		timer.Simple(1,self.Owner.EmitSound,self.Owner,self.Sounds.ClipIn);
		timer.Simple(2,self.Owner.EmitSound,self.Owner,self.Sounds.SlideBack);

	end

end

function SWEP:SecondaryAttack()

	if SERVER then

		self.CurMode = self.CurMode + 1;

		if self.CurMode == 4 then

			self.CurMode = 1;

		end

		self.Weapon:SetNWInt("smod.CurMode",self.CurMode);

		self.Weapon:SetNextPrimaryFire(CurTime());

	end

	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK);

	//delay the fire
	self.Weapon:SetNextSecondaryFire(CurTime() + 0.2);

	//return
	return false;

end

SWEP.STime = 0

function SWEP:GetViewModelPosition(Pos,Ang)

	local VOffset1 = Vector(-5.1443,-2.6841,0.4343);
	local AOffset1 = Vector(0,0,0);

	local VOffset2 = Vector(-4.588, -5.4669, -1.3345);
	local AOffset2 = Vector(9.8865, 0.1932, 0);

	local VOffset3 = Vector(-6.5165, -6.7945, 1.9816);
	local AOffset3 = Vector(1.7223, 0.002, 0);

	local OldCurMode = self.CurMode;
	self.CurMode = self.Weapon:GetNWInt("smod.CurMode",1);

	self.PosOffset = self.PosOffset or VOffset1;
	self.AngOffset = self.AngOffset or AOffset1;

	if OldCurMode != self.CurMode then

		self.STime = CurTime();

		if self.CurMode == 1 then

			self.PosOffset = VOffset1;
			self.AngOffset = AOffset1;
			
			self.BobScale = 1;
			self.SwayScale = 1;

		elseif self.CurMode == 2 then

			self.PosOffset = VOffset2;
			self.AngOffset = AOffset2;
			
			self.BobScale = 0.8;
			self.SwayScale = 0.8;

		elseif self.CurMode == 3 then

			self.PosOffset = VOffset3;
			self.AngOffset = AOffset3;

			self.BobScale = 0.1;
			self.SwayScale = 0.1;
			
		end

	end

	local Speed = 0.2

	local Frac = ((self.STime + Speed) - CurTime()) / Speed
	Frac = math.Clamp(Frac,0,1);

	Frac = 1 - Frac;

	Ang:RotateAroundAxis(Ang:Right(),self.AngOffset.x * Frac);
	Ang:RotateAroundAxis(Ang:Forward(),self.AngOffset.y * Frac);
	Ang:RotateAroundAxis(Ang:Up(),self.AngOffset.z * Frac);

	Pos = Pos + self.PosOffset.x * Ang:Right() * Frac;
	Pos = Pos + self.PosOffset.y * Ang:Forward() * Frac;
	Pos = Pos + self.PosOffset.z * Ang:Up() * Frac;

	return Pos,Ang;

end

//when we deploy
function SWEP:Deploy()

	//run the deploy animation
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

	//and return
	return true;

end